var flat = (arr) => {
  const resArr = []
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      resArr.push(...flat(arr[i]))
    } else {
      resArr.push(arr[i])
    }
  }
  return resArr
}

flat = function (arr) {
  return arr.reduce((prev, cur) => {
    return prev.concat(Array.isArray(cur) ? flat(cur) : cur)
  }, [])
}
flat = (arr, num = 1) => {
  const resArr = []
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      if (num - 1 >= 0) {
        console.log(num);
        resArr.push(...flat(arr[i], num - 1))
      } else {
        resArr.push(arr[i])
      }
    } else {
      resArr.push(arr[i])
    }
  }
  return resArr
}

flat = function (arr, dep) {
  return arr.reduce((prev, cur) => {
    return prev.concat(Array.isArray(cur) && dep >= 0 ? flat(arr, dep - 1) : cur)
  }, [])
}





const arr = [1, 2, 3, 4, [5, 6, 7, [55, 66]], ['123', { a: 1 }]]

const res = flat(arr, Infinity)
console.log(res);